package pingce.share.model;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import pingce.share.model.raw.Sentence;
import pingce.share.util.*;

/**
 * 存放每个句子的处理结果。
 * @author ucai
 *
 */
public class SentenceResult implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = -7006980658160435569L;
	private int weiboID;
	private int sentenceID;
//	begin和end分别代表该句在整条微博中的起始和结束位置（字符的offset）；rawText是句子的原始文本
	private int begin;
	private int end;
	private String rawText;
	private LinkedList<SubSentenceResult> subSentences;
	
	public SentenceResult(int weiboID, Sentence sentence, 
			SentenceProcessor sentenceProcessor, WrappedSplitter ws){
		this.weiboID = weiboID;
		this.sentenceID = sentence.id;
		this.begin = sentence.begin;
		this.end = sentence.begin + sentence.text.length() - 1;
		this.rawText = sentence.text;
		this.subSentences = new LinkedList<SubSentenceResult>();
//		句子在处理之前需要使用WrappedSplitter将句子分成子句，每个子句单独进行分词和句法分析
		List<PairValue> subTexts = ws.Split(rawText, true);
		int offset = 0;
		for(PairValue pv : subTexts){
			String subText = pv.getBasic();
			int subTextLen = subText.length();
			SubSentenceResult ssr = new SubSentenceResult(begin + offset, pv, sentenceProcessor);
			this.subSentences.add(ssr);
			offset += subTextLen;
		}
	}
	
	public int getWeiboID() {
		return weiboID;
	}

	public void setWeiboID(int weiboID) {
		this.weiboID = weiboID;
	}

	public int getSentenceID() {
		return sentenceID;
	}

	public void setSentenceID(int sentenceID) {
		this.sentenceID = sentenceID;
	}

	public int getBegin() {
		return begin;
	}

	public void setBegin(int begin) {
		this.begin = begin;
	}

	public int getEnd() {
		return end;
	}

	public void setEnd(int end) {
		this.end = end;
	}

	public String getRawText() {
		return rawText;
	}

	public void setRawText(String rawText) {
		this.rawText = rawText;
	}

	public LinkedList<SubSentenceResult> getSubSentences() {
		return subSentences;
	}

	public void setSubSentences(LinkedList<SubSentenceResult> subSentences) {
		this.subSentences = subSentences;
	}
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(this.begin + ":" + this.end + "\t" 
				+ this.weiboID + ":" + this.sentenceID + "\n"
				+ this.rawText + "\n");
		for(SubSentenceResult ssr : this.subSentences){
			sb.append(ssr.toString() + "\n");
		}
		return sb.toString();
	}
}
